﻿using Core.ECS.Systems;
using Core.Log;
using Core.Rendering.Resources;
using Core.Tools;
using OpenTK.Mathematics;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.ECS.Components
{
    public class TestCameraBehavior : CBehavior
    {
        public Actor player;
        public int playerX;
        public int playerY;
        public Actor[,] Actors = new Actor[10, 10];
        public int[,] maps =
        {
            { 1,1,1,1,1,1,1,1,1,1 },
            { 1,0,0,0,0,0,0,0,0,1 },
            { 1,0,1,0,0,0,0,1,0,1 },
            { 1,0,0,0,0,1,0,0,0,1 },
            { 1,0,2,0,0,0,0,1,0,1 },
            { 1,0,0,0,0,1,0,3,0,1 },
            { 1,0,0,2,0,2,0,0,0,1 },
            { 1,0,0,0,0,0,0,0,0,1 },
            { 1,0,1,0,0,0,2,0,0,1 },
            { 1,1,1,1,1,1,1,1,1,1 }
        };
        public override void Start()
        {
            Scene.Instance.MainCamera.ClearColor = Color4.White;
            Material materialBlack = Material.Create();
            materialBlack.Texture = Texture2D.Create(Color4.Black);
            Material materialLightPink = Material.Create();
            materialLightPink.Texture = Texture2D.Create(Color4.LightPink);
            Material materialBlue = Material.Create();
            materialBlue.Texture = Texture2D.Create(Color4.Blue);

            for (int i = 0; i < 10; i++)
            {
                for(int j = 0; j < 10; j++)
                {
                    if (maps[i, j] == 1)
                    {
                        Actor actor = Actor.CreateActor();
                        Actors[i, j] = actor;
                        Actors[i, j].Transform.LocalPosition = new Vector3(i, j, 0);
                        Actors[i, j].Renderer.Material = materialBlack;
                    }
                    else
                    if (maps[i,j] == 2)
                    {
                        Actors[i, j] = Actor.CreateActor();
                        Actors[i, j].Transform.LocalPosition = new Vector3(i, j, 0);
                        Actors[i, j].Renderer.Material = materialLightPink;
                    }
                    else if (maps[i, j] == 3)
                    {
                        maps[i, j] = 0;
                        player = Actor.CreateActor();
                        player.Transform.LocalPosition = new Vector3(i, j, 0);
                        playerX = i;
                        playerY = j;
                        player.Renderer.Material = materialBlue;
                    }
                }
            }
            Transform.LocalPosition = player.Transform.LocalPosition;
            Transform.LocalPosition -= Vector3.UnitZ * 10;
            Transform.Parent = player.Transform;
        }
        public override void Update()
        {
            if (Input.GetKeyPressed(Keys.W))
            {
                Move(0, 1);
            }
            else if (Input.GetKeyPressed(Keys.S))
            {
                Move(0, -1);
            }
            else if (Input.GetKeyPressed(Keys.A))
            {
                Move(1, 0);
            }
            else if (Input.GetKeyPressed(Keys.D))
            {
                Move(-1, 0);
            }
        }
        public void Move(int movex,int movey)
        {
            int x = playerX + movex;
            int y = playerY + movey;
            if (x < 0 || y < 0 || x > 9 || y > 9) return;
            if (maps[x,y] == 0)
            {
                player.Transform.LocalPosition += Vector3.UnitX * movex;
                player.Transform.LocalPosition += Vector3.UnitY * movey;
                playerX = x;
                playerY = y;
            }else if (maps[x,y] == 2)
            {
                int x1 = x + movex;
                int y1 = y + movey;
                if (x1 < 0 || y1 < 0 || x1 > 9 || y1 > 9) return;
                if (maps[x1,y1] == 0)
                {
                    //角色加相机移动
                    player.Transform.LocalPosition += Vector3.UnitX * movex;
                    player.Transform.LocalPosition += Vector3.UnitY * movey;
                    playerX = x;
                    playerY = y;
                    //箱子移动
                    Actors[x1, y1] = Actors[x, y];
                    Actors[x, y] = null;
                    maps[x1, y1] = 2;
                    maps[x, y] = 0;
                    Actors[x1, y1].Transform.LocalPosition += Vector3.UnitX * movex;
                    Actors[x1, y1].Transform.LocalPosition += Vector3.UnitY * movey;
                }
            }
        }
    }
}
